package org.roger.search;

import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanTermQuery;

/**
 *  跨度查询
 * @param field 搜索的字段 
 * @param phrase 短语数组(表示要包含跨度的对象,短语的前后顺序不可变)
 * @param nearIndex 域的邻近几个范围内跨度
 * @author luoweiyi
 */
public class SpanNearSearch extends LuceneSearchBase{
	
	private String field;
	private String[] phrase;
	private int nearIndex;

	public SpanNearSearch(String field,String[] phrase,int nearIndex){
		this.field = field;
		this.phrase = phrase;
		this.nearIndex = nearIndex;
	}
	
	@Override
	public Query getSearchQuery() {
		SpanTermQuery[] querys = new SpanTermQuery[phrase.length];
		for(int i = 0; i < querys.length; i++){
			querys[i] = new SpanTermQuery(new Term(field,phrase[i]));
		}
		SpanNearQuery query = new SpanNearQuery(querys,nearIndex,true);
		
		return query;
	}
	
	public static void main(String[] args) {
		String[] phrase = {"quality","vigor"};
		SpanNearSearch search = new SpanNearSearch("contents",phrase,10);
		try {
			search.search("D:/index2");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public Sort getSort() {
		// TODO Auto-generated method stub
		return null;
	}

}
